package com.example.h5shell.utils;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

import androidx.core.app.NotificationCompat;

import com.example.h5shell.R;

/**
 * desc: 增加通知信息
 * use:   NotificationHelper notificationHelper = new NotificationHelper(this);
 *         notificationHelper.sendNotification("Hello Notification", "This is a test notification!", MainActivity.class);
 */
public class NotificationHelper {

    private static final String CHANNEL_ID = "my_notification_channel";
    private static final String CHANNEL_NAME = "My Notification Channel";

    private Context context;

    public NotificationHelper(Context context) {
        this.context = context;
    }

    public void sendNotification(String title, String message, Class<?> targetActivity) {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
                    CHANNEL_NAME,
                    NotificationManager.IMPORTANCE_HIGH);
            notificationManager.createNotificationChannel(channel);
        }

        Intent intent = new Intent(context, targetActivity); // 替换YourActivity为你想要启动的Activity
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
                .setSmallIcon(R.mipmap.ic_launcher) // 设置通知图标
                .setContentTitle(title) // 设置通知标题
                .setContentText(message) // 设置通知内容
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setContentIntent(pendingIntent) // 设置点击通知后的跳转意图
                .setAutoCancel(true); // 设置点击通知后自动消失

        Notification notification = builder.build();
        int notificationId = 1; // 你可以根据需要修改这个ID，或者让它成为一个动态生成的ID
        notificationManager.notify(notificationId, notification);
    }
}